package com.fancy.learncenter.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.PopupUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;
import com.fancy.learncenter.view.PopupWindowCutPic;
import com.fancy.teeya.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.app.TakePhotoImpl;
import com.jph.takephoto.model.InvokeParam;
import com.jph.takephoto.model.TContextWrap;
import com.jph.takephoto.model.TResult;
import com.jph.takephoto.permission.InvokeListener;
import com.jph.takephoto.permission.PermissionManager;
import com.jph.takephoto.permission.TakePhotoInvocationHandler;
import com.jph.takephoto.uitl.TConstant;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class DailyBeautyFragment extends Fragment {


    private TakePhoto takePhoto;
    private InvokeParam invokeParam;

    @Bind(R.id.scan)
    Button scan;
    @Bind(R.id.corp)
    Button corp;

    View view;
    //扫描二维码
    IntentIntegrator integrator;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_beauty, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        LogUtil.MyLog("tupian", requestCode + "=======requestCode===");
        if (requestCode == IntentIntegrator.REQUEST_CODE) {
            IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
            if (intentResult != null) {
                ToastUtil.show("扫描结果为：" + intentResult.getContents());
            } else {
                ToastUtil.show("扫描结束");
            }
        } else if (requestCode == TConstant.RC_PICK_PICTURE_FROM_GALLERY_CROP
                || requestCode == TConstant.RC_PICK_PICTURE_FROM_CAPTURE_CROP
                || requestCode == TConstant.RC_CROP) {
            getTakePhoto().onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.scan, R.id.corp})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.scan:
                IntentIntegrator integrator = IntentIntegrator.forSupportFragment(DailyBeautyFragment.this);
                integrator.setPrompt("测试");
                integrator.setBeepEnabled(true);
                integrator.setBarcodeImageEnabled(true);
                integrator.initiateScan();
                break;
            case R.id.corp:
                final PopupWindowCutPic popupWindowCutPic = new PopupWindowCutPic(view);
                popupWindowCutPic.setOnClickListener(new PopupWindowCutPic.CutPicCallBack() {
                    @Override
                    public void choose() {
//                        ToastUtil.show("选择相册");
                        takePhoto = getTakePhoto();
                        takePhoto.onPickFromGalleryWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                    }

                    @Override
                    public void cancel() {
                        popupWindowCutPic.dismiss();
                    }

                    @Override
                    public void camera() {
                        takePhoto = getTakePhoto();
                        takePhoto.onPickFromCaptureWithCrop(Utils.getCutPicPath(), Utils.getCutPicCropOptions());
                    }

                    @Override
                    public void see() {
                        ToastUtil.show("查看大图");
                    }
                });
                popupWindowCutPic.isShow();

                break;
        }
    }


    /**
     * 获取TakePhoto实例
     *
     * @return
     */

    public TakePhoto getTakePhoto() {
        if (takePhoto == null) {
            takePhoto = (TakePhoto) TakePhotoInvocationHandler.of(new InvokeListener() {
                @Override
                public PermissionManager.TPermissionType invoke(InvokeParam invokeParam) {
                    PermissionManager.TPermissionType type = PermissionManager.checkPermission(TContextWrap.of(getActivity()), invokeParam.getMethod());
                    return type;
                }
            }).bind(new TakePhotoImpl(this, new TakePhoto.TakeResultListener() {
                @Override
                public void takeSuccess(TResult result) {
//                    LogUtil.MyLog("tupian", "=======takeSuccess===" + result.getImage().getPath());

                    ToastUtil.show("=======takeSuccess===" + result.getImage().getPath());
                }

                @Override
                public void takeFail(TResult result, String msg) {
//                    LogUtil.MyLog("tupian", "=======takeFail===" + result.toString());

                    ToastUtil.show("=======takeFail===" + result.toString());
                }

                @Override
                public void takeCancel() {
//                    LogUtil.MyLog("tupian", "=======takeCancel===");

                    ToastUtil.show("=======takeCancel===");
                }
            }));
        }
        return takePhoto;
    }

}
